package org.healer.simple_juc;

import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
    public static void main(String[] args) {
        TestLock2 tl2 = new TestLock2();
        new Thread(tl2).start();
        new Thread(tl2).start();
        new Thread(tl2).start();
    }
}
class TestLock2 implements Runnable{
    private int ticketNum = 10;
    private final ReentrantLock lock=new ReentrantLock();
    @Override
    public void run() {
        while(true){
                try {
                    lock.lock();
                    if(ticketNum>0){
                        System.out.println(Thread.currentThread().getName()+"--->"+ticketNum--);
                    }else break;
                }
                finally {
                        lock.unlock();
                }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}